// 注册与登录
var InterValObj; //timer变量，控制时间
var curCount;//当前剩余秒数
$(function(){

// ------------------------------------------------------------------------ 登录js
	// 登录验证
	$('#login_btn').on('click',function(e){
		e.preventDefault();
		var flag = false;
		$('#login_form [placeholder]').each(function(){
			if($(this).val() == ''){
				zk_alert_msg($(this).attr('placeholder'));
				flag = true;return false;
			}
		});
		if(flag){
			return;
		}
		var post_data = $('#login_form').serialize();
		var url = '/Api/Login';

	    //do_login_fun(url,post_data);

		$.ajax({
		    type: 'post',
		    url: url,
		    dataType: 'json',
		    data: post_data,
		    success: function (data) {
		        if (data.Code == 1) {
		            var storage = window.localStorage;
		            storage.setItem('token', data.Data);

		            zk_alert_msg(data.Msg, 3, function () {
		                location.href = '/wap/member.html';
		            });
		        } else {
		            zk_alert_msg(data.Msg, 0);
		        }
		        return;
		    }
		});
	});


// ------------------------------------------------------------------------ 短信验证登录js
	$('#dxyz_login_btn').on('click',function(e){
		e.preventDefault();
		var flag = false;
		$('#dxyz_login_form [placeholder]').each(function(){
			if($(this).val() == ''){
				zk_alert_msg($(this).attr('placeholder'));
				flag = true;return false;
			}
		});
		if(flag){
			return;
		}
		var post_data = $('#dxyz_login_form').serialize();
		var url = '/Api/PhoneLogin';
		//do_login_fun(url,post_data);

        $.ajax({
		    type: 'post',
		    url: url,
		    dataType: 'json',
		    data: post_data,
		    success: function (data) {
		        if (data.Code == 1) {
		            var storage = window.localStorage;
		            storage.setItem('token', data.Data);

		            zk_alert_msg(data.Msg, 3, function () {
		                location.href = '/wap/member.html';
		            });
		        } else {
		            zk_alert_msg(data.Msg, 0);
		        }
		        return;
		    }
        });
	});


// ------------------------------------------------------------------------ 注册js
	// 注册验证
	$('#reg_btn').on('click',function(e){
		e.preventDefault();
		if( ! $('#zk_xy_box').is(':checked')){
			zk_alert_msg('请先同意注册协议');return;
		}
		var flag = false;
		$('#register_form [placeholder]').each(function(){
			if($(this).val() == ''){
				zk_alert_msg($(this).attr('placeholder'));
				flag = true;return false;
			}
		});
		if(flag){
			return;
		}
		if( ! zk_verify_mobile($('#mob').val())){
			zk_alert_msg('请输入正确的手机号码');return;
		}
		//var pwd = $('#pwd').val();
		//if( ! zk_is_pwd(pwd)){
		//	zk_alert_msg('密码长度必须是6位数以上的数字和字母组成');return;
		//}
		var post_data = $('#register_form').serialize();
                $.ajax({
                    type: "post",
                    url: "/Api/Register",
                    data: post_data,
                    success: function (data) {
                        if (data.Code == 1) 
                        {
                            zk_alert_msg(data.Msg, 3, function () {
                                location.href = "login.html";
                            });
                            return;
                        }
                        else if(data.Code==2)
                        {
                            zk_alert_msg(data.Msg, 3, function () {
                                location.href = "sms_vcode_login.html";
                            });
                        }
                        else
                        {
                            zk_alert_msg(data.Msg);
                            return;
                        }
                    }
                })
	});


// ------------------------------------------------------------------------ 获取验证码js
	// 获取验证码
	$(document).on('click','#get_mob_yzm_btn',function(){
        var mobile = $('#mob').val();
        if( ! zk_verify_mobile(mobile)){
			zk_alert_msg('请输入正确的手机号码');return;
		}
		if(InterValObj && curCount > 0){
			return;
		}
        curCount=59;
        //设置button效果，开始计时
        $(this).attr('disabled', 'true');
        $(this).text(curCount + 's后重新获取');
        InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一
        $.ajax({
            url: "/Api/SendSms",
            type:'post',
            dataType: 'json',
            data:{phone:mobile},
            success: function (data) {
                if(data.Code==1)
                {
                    zk_alert_msg(data.Msg, 3);
                }
                else
                {
                    zk_alert_msg("发送验证码失败，请联系管理员", 0);
                }
                return;
            }
        });
    });

 // ------------------------------------------------------------------------ 忘记密码js
    //忘记密码下一步
    $("#next_step1").click(function (e) {
        e.preventDefault();
        var mobile = $("#mob").val();
        var authCode = $("#authCode").val();

        if (!zk_verify_mobile(mobile)) {
            zk_alert_msg('请输入正确的手机号码');
            return;
        }
        if (authCode == '' || authCode == null) {
            zk_alert_msg('请输入验证码！');
            return;
        }

        $.ajax({
            type:'get',
            url:'/Login/do_forget_next',
            data:{mobile:mobile,authCode:authCode},
            dataType:'json',
            success:function(res){
                if(res.status==1){
                    location.href='/wap/reset_password.html';
                }
                else {
                    zk_alert_msg(res.msg);
                }
            }
        });

    });
    //短信验证下一步
    $('#next_step2').click(function (e) {
        e.preventDefault();
        var yzm = $('#yzm').val();

        if (yzm == '' || yzm == null) {
            zk_alert_msg('请输入短信验证码！');
            return;
        }
        $.ajax({
            type: 'get',
            url: '/Login/do_msg_next',
            data: {yzm: yzm},
            dataType: 'json',
            success: function (res) {
                if (res.status == 1) {
                    location.href = '/wap/reset_success.html';
                }
                else {
                    zk_alert_msg(res.msg);
                }
            }
        });
    });
    //重置密码
    $("#reset_pwd").click(function(e){
        e.preventDefault();

        var pwd = $('#pwd').val();
        var repwd = $('#repwd').val();
        if( ! zk_is_pwd(pwd)){
            zk_alert_msg('密码长度必须是6位数以上的数字和字母组成');return;
        }
        if(pwd != repwd){
            zk_alert_msg('两次密码不一致');return;
        }

        $.ajax({
            type: 'get',
            url: '/Login/do_reset_password',
            data: {pwd: pwd},
            dataType: 'json',
            success: function (res) {
                if (res.status == 1) {
                    layer.open({
                        content:res.msg,
                        time:3,
                        end:function(){
                            location.href = '/wap/login.html';
                        }
                    });
                }
                else {
                    zk_alert_msg(res.msg);
                }
            }
        });
    });
});
// timer处理函数
var SetRemainTime = function(){
    if(curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $('#get_mob_yzm_btn').removeAttr('disabled');//启用按钮
        $('#get_mob_yzm_btn').text('重新发验证码');
    }else{
        curCount--;
        $('#get_mob_yzm_btn').text(curCount + 's后重新获取');
    }
};
// 统一ajax处理登录与注册的请求
var do_login_fun = function(post_url,post_data){
	$.ajax({
		type:'post',
        url:post_url,
        dataType:'json',
        data:post_data,
        success:function(data){
        	if(data.Code == 1){
                zk_alert_msg(data.Msg,3,function(){
                    location.href = data.Data;
                });
        	}else{
        		zk_alert_msg(data.Msg, 0);
        	}
            return;
        }
    });
};